package com.pylc.webapp.action.admin;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.pylc.model.entity.Area;
import com.pylc.service.AreaService;
import com.pylc.webapp.action.BaseAction;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Lindsey
 */
@ParentPackage("admin")
public class AreaAction extends BaseAction {
    private Area area;
    private Long parentId;
    private Area parent;
    private Set<Area> areas = new HashSet<Area>();

    @Resource
    private AreaService areaService;

    // 添加
    public String add() {
        if (parentId != null) {
            parent = areaService.load(parentId);
        }
        return INPUT;
    }

    // 编辑
    public String edit() {
        area = areaService.load(id);
        parent = area.getParent();
        return INPUT;
    }

    // 列表
    public String list() {
        if (parentId != null) {
            parent = areaService.load(parentId);
            areas = parent.getChildren();
        } else {
            areas = areaService.getRoot();
        }
        return LIST;
    }

    // 删除
    public String delete() {
        areaService.delete(id);
        return ajaxJsonSuccessMessage("删除成功！");
    }

    // 保存
    @Validations(
            requiredStrings = {
                    @RequiredStringValidator(fieldName = "area.name", message = "地区名称不允许为空!")
            }
    )
    @InputConfig(resultName = "error")
    public String save() {
        String newName = area.getName();
        if (!areaService.isNameUnique(parentId, newName)) {
            addActionError("地区名称已存在!");
            return ERROR;
        }
        if (parentId != null) {
            area.setParent(areaService.load(parentId));
        } else {
            area.setParent(null);
        }
        areaService.save(area);
        redirectionUrl = "area!list?parentId=" + parentId;
        return SUCCESS;
    }

    // 更新
    @Validations(
            requiredStrings = {
                    @RequiredStringValidator(fieldName = "area.name", message = "地区名称不允许为空!")
            }
    )
    public String update() {
        Area persistent = areaService.load(id);
        Area parent = persistent.getParent();
        if (parent != null) {
            parentId = parent.getId();
        }
        if (!areaService.isNameUnique(parentId, area.getName())) {
            addActionError("地区名称已存在!");
            return ERROR;
        }
        persistent.setName(area.getName());
        areaService.update(persistent);
        redirectionUrl = "area!list?parentId=" + parentId;
        return SUCCESS;
    }

    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }

    public Set<Area> getAreas() {
        return areas;
    }

    public Area getParent() {
        return parent;
    }

    public void setParent(Area parent) {
        this.parent = parent;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }
}
